/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditarPeca.java
 *
 * Created on 05/04/2011, 02:12:37
 */
package Tela;

import Classe.BD;
import Classe.Fornecedor;
import Classe.Peca;
import Classe.Validacao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import localizarEditar.LocalizarFornecedor;

/**
 *
 * @author gladson
 */
public class CadastroPeca extends Validacao {

    /** Creates new form EditarPeca */
    public CadastroPeca() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        txtNomeForn.setEnabled(false);

    }
    int idpeca;
    int test = 0;
    int idFornecedor;
    Fornecedor fonece;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelCadastroPeca = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtnomePeca = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtMarcaPeca = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtModeloPeca = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescPeca = new javax.swing.JTextArea();
        btLocFornPec = new javax.swing.JButton();
        txtNomeForn = new javax.swing.JTextField();
        btIncluirPeca = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        panelCadastroPeca.setBackground(new java.awt.Color(255, 255, 255));

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Cadastro Peça ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("PMingLiU", 0, 36))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel2.setText("Nome");

        txtnomePeca.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel3.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel3.setText("Marca");

        txtMarcaPeca.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel4.setText("Modelo");

        txtModeloPeca.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel5.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel5.setText("Descrição");

        txtDescPeca.setColumns(20);
        txtDescPeca.setRows(5);
        txtDescPeca.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jScrollPane2.setViewportView(txtDescPeca);

        btLocFornPec.setText("Inserir");
        btLocFornPec.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btLocFornPec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLocFornPecActionPerformed(evt);
            }
        });

        txtNomeForn.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        btIncluirPeca.setText("Salvar");
        btIncluirPeca.setToolTipText("Salvar");
        btIncluirPeca.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btIncluirPeca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btIncluirPecaActionPerformed(evt);
            }
        });

        jButton4.setText("Sair");
        jButton4.setToolTipText("Sair");
        jButton4.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel1.setText("Razão Social");

        jButton1.setText("Limpar");
        jButton1.setToolTipText("Limpar");
        jButton1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel4))
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMarcaPeca, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                    .addComponent(txtnomePeca, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                    .addComponent(txtModeloPeca, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(txtNomeForn, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btLocFornPec))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                        .addComponent(btIncluirPeca)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4)))
                .addContainerGap())
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btIncluirPeca, jButton1, jButton4});

        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNomeForn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btLocFornPec))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtnomePeca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtMarcaPeca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtModeloPeca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btIncluirPeca)
                    .addComponent(jButton1)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btIncluirPeca, jButton1, jButton4});

        javax.swing.GroupLayout panelCadastroPecaLayout = new javax.swing.GroupLayout(panelCadastroPeca);
        panelCadastroPeca.setLayout(panelCadastroPecaLayout);
        panelCadastroPecaLayout.setHorizontalGroup(
            panelCadastroPecaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCadastroPecaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelCadastroPecaLayout.setVerticalGroup(
            panelCadastroPecaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCadastroPecaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelCadastroPeca, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelCadastroPeca, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-808)/2, (screenSize.height-627)/2, 808, 627);
    }// </editor-fold>//GEN-END:initComponents

    private void btLocFornPecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLocFornPecActionPerformed
        LocalizarFornecedor locForn = new LocalizarFornecedor();
        locForn.recebePeca(this);
        locForn.setVisible(true);
}//GEN-LAST:event_btLocFornPecActionPerformed

    private void btIncluirPecaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btIncluirPecaActionPerformed
        if (validar()) {

            try {
                Peca peca = new Peca();

                peca.setNomePeca(txtnomePeca.getText().trim().toUpperCase());
                peca.setMarcaPeca(txtMarcaPeca.getText().trim().toUpperCase());
                peca.setModeloPeca(txtModeloPeca.getText().trim().toUpperCase());


                peca.setDescPeca(txtDescPeca.getText().trim().toUpperCase());

                String sql = "INSERT INTO PECA (ID_PECA,NOME_PECA,MARCA_PECA,MODELO_PECA,DESC_PECA, ID_FORNECEDOR) VALUES(SQ_PECA.NEXTVAL,?,?,?,?,?)";
                Object[] parametros = {peca.getNomePeca(), peca.getMarcaPeca(), peca.getModeloPeca(), peca.getDescPeca(), fonece.getIdFornecedor()};
                Object[] tipos = {String.class, String.class, String.class, String.class, Integer.class};

                BD banco = new BD();
                banco.executar(sql, parametros, tipos);
                JOptionPane.showMessageDialog(rootPane, "Cadastro salvo com Sucesso");

                this.dispose();
                CadastroPeca cadpeca = new CadastroPeca();
                cadpeca.setVisible(true);
            } catch (Exception ex) {
            }

        }
}//GEN-LAST:event_btIncluirPecaActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.dispose();
}//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
        CadastroPeca cadpeca = new CadastroPeca();
        cadpeca.setVisible(true);
// TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadastroPeca().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btIncluirPeca;
    private javax.swing.JButton btLocFornPec;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel panelCadastroPeca;
    private javax.swing.JTextArea txtDescPeca;
    private javax.swing.JTextField txtMarcaPeca;
    private javax.swing.JTextField txtModeloPeca;
    private javax.swing.JTextField txtNomeForn;
    private javax.swing.JTextField txtnomePeca;
    // End of variables declaration//GEN-END:variables

    public void IniciarEditar(int idPeca) {
        try {
            txtNomeForn.setEnabled(false);
            idpeca = idPeca;
            String sql = "SELECT P.NOME_PECA, P.MARCA_PECA, P.MODELO_PECA, P.DESC_PECA, P.ID_FORNECEDOR, F.RAZAO_FORNECEDOR FROM PECA P "
                    + "INNER JOIN FORNECEDOR F ON P.ID_FORNECEDOR=F.ID_FORNECEDOR WHERE ID_PECA=" + idpeca;
            BD bd = new BD();
            bd.consultar(sql);
            ResultSet r = bd.consultar(sql);

            if (r.next()) {
                txtnomePeca.setText(r.getString("NOME_PECA"));
                txtMarcaPeca.setText(r.getString("MARCA_PECA"));
                txtModeloPeca.setText(r.getString("MODELO_PECA"));
                txtDescPeca.setText(r.getString("DESC_PECA"));
                txtNomeForn.setText(r.getString("RAZAO_FORNECEDOR"));
                idFornecedor = Integer.parseInt(r.getString("ID_FORNECEDOR"));

            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CadastroPeca.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CadastroPeca.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void preecherForn(Fornecedor forn) {
        test = 1;
        fonece = new Fornecedor();
        this.fonece = forn;
        txtNomeForn.setText(forn.getRazaoSocialFornecedor());

    }

    public boolean validar() {
        boolean Resultado = true;

        if (validarNomePeca(txtnomePeca.getText().trim()) == false) {
            Resultado = false;
        } else if (validarMarca(txtMarcaPeca.getText().trim()) == false) {
            Resultado = false;
        } else if (validarModelo(txtModeloPeca.getText().trim()) == false) {
            Resultado = false;
        } else if (validarObservacao(txtDescPeca.getText().trim()) == false) {
            Resultado = false;
        } else if (test == 0) {
            JOptionPane.showMessageDialog(rootPane, "Obrigatório inserir um  do Fornecedor");
        }

        return Resultado;
    }
}
